package com.spogher.bsc.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.BscSession;
import com.spogher.bsc.data.dao.interfaces.ManagerDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Manager;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.components.dropdownChoices.ManagerDropDownChoice;

public class TeamPage extends BscTemplatePage{
	
	private static final String NO_CHAMPIONSHIP_AFFECTED_YET = "no championship affected yet";
	
	@SpringBean
	private TeamDao teamDao;
	@SpringBean
	private ManagerDao managerDao;

    public	TeamPage() {
    	super();
		Form<Team> teamForm = new Form<Team>("teamForm", new CompoundPropertyModel<Team>(new Team()));
		teamForm.add(new TextField<String>("name").setRequired(true));
		teamForm.add(new TextField<String>("coach").setRequired(true));
		List<Manager> managerList = new ArrayList<Manager>();
		BscSession session = BscSession.get();
		if (session.isRootUser()){
			managerList = managerDao.findAll();
		}
		else {
			managerList = new ArrayList<Manager>();
			if (BscSession.get().isAuthenticated()) {			
				managerList.add(session.getManager());
			}
		}
		DropDownChoice<Manager> managerChoice = new ManagerDropDownChoice("manager", managerList);
		teamForm.add(managerChoice);
		
		final WebMarkupContainer wmc = new WebMarkupContainer("listContainer");
		
		wmc.add(new ListView<Team>("list", new PropertyModel<List<Team>>(this, "teamDao.findAll")){

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Team> item) {
				Team team = item.getModelObject();
				item.add(new Label("teamName", team.getName()));
				item.add(new Label("teamCoach", team.getCoach()));
				item.add(new Label("teamManager", team.getManager().getName()));
				if (team.getChampionship() != null) {
					item.add(new Label("teamChampionship", team.getChampionship().getName()));
				}
				else {
					item.add(new Label("teamChampionship", NO_CHAMPIONSHIP_AFFECTED_YET));
				}
			}
			
		});
		wmc.setOutputMarkupId(true);
		add(wmc);
		
		teamForm.add(new AjaxSubmitLink("submit"){
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Team team = (Team) form.getModelObject();
				Team newTeam = new Team();
				newTeam.setName(team.getName());
				newTeam.setCoach(team.getCoach());
				newTeam.setManager(team.getManager());
				teamDao.save(newTeam);
				target.addComponent(wmc);
			}
		});
		
		
		add(teamForm);
    }
}
